package com.group3.hospital.controller;/*
 *@author 时崎
 * 2022/01/04
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.hospital.bean.Subject;
import com.group3.hospital.service.SubjectService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class SubjectManageController {
    @Resource
    private SubjectService subjectService;
    //显示非手术科室管理页面
    @RequestMapping("/noOpeUi")
    public String noOpeUi(Model model,@RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                          @RequestParam(defaultValue = "5",value = "pageSize") Integer pageSize){
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        PageHelper.startPage(pageNum,pageSize);
        try {
            List<Subject> nooperative = subjectService.showNoOpe();
            PageInfo<Subject> pageInfo = new PageInfo<>(nooperative,pageSize);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("nooperative",nooperative);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "dep_manage1";
    }
    //显示手术科室管理页面
    @RequestMapping("/opeUi")
    public String opeUi(Model model,@RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                        @RequestParam(defaultValue = "5",value = "pageSize") Integer pageSize){
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        PageHelper.startPage(pageNum,pageSize);
        try {
            List<Subject> operative = subjectService.showOpe();
            PageInfo<Subject> pageInfo = new PageInfo<>(operative,pageSize);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("operative",operative);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "dep_manage2";
    }
    //显示诊断科室管理页面
    @RequestMapping("/diagnosisUi")
    public String diagnosisUi(Model model,@RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                              @RequestParam(defaultValue = "5",value = "pageSize") Integer pageSize){
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        PageHelper.startPage(pageNum,pageSize);
        try {
            List<Subject> diagnosis = subjectService.showDiagnosis();
            PageInfo<Subject> pageInfo = new PageInfo<>(diagnosis,pageSize);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("diagnosis",diagnosis);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "dep_manage3";
    }

    //根据id删除诊断科室
    @RequestMapping("/deleteDiaSub")
    public String deleteDiaSub(@RequestParam("subjectId") Integer subjectId){
        try {
            subjectService.deleteById(subjectId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/diagnosisUi";
    }
    //根据id删除手术科室
    @RequestMapping("/deleteOpeSub")
    public String deleteOpeSub(@RequestParam("subjectId") Integer subjectId){
        try {
            subjectService.deleteById(subjectId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/opeUi";
    }
    //根据id删除非手术科室
    @RequestMapping("/deleteNoOpeSub")
    public String deleteNoOpeSub(@RequestParam("subjectId") Integer subjectId){
        try {
            subjectService.deleteById(subjectId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/noOpeUi";
    }
    //更改科室介绍1
    @RequestMapping("/updateSub")
    public String updateSub(@RequestParam("subjectId") Integer subjectId,@RequestParam("subjectDescription")String subjectDescription){
        Subject sub = new Subject();
        sub.setSubjectId(subjectId);
        sub.setSubjectDescription(subjectDescription);
        try {
            subjectService.update(sub);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:noOpeUi";
    }
    //更改科室介绍2
    @RequestMapping("/updateSub2")
    public String updateSub2(@RequestParam("subjectId") Integer subjectId,@RequestParam("subjectDescription")String subjectDescription){
        Subject sub = new Subject();
        sub.setSubjectId(subjectId);
        sub.setSubjectDescription(subjectDescription);
        try {
            subjectService.update(sub);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:opeUi";
    }
    //更改科室介绍3
    @RequestMapping("/updateSub3")
    public String updateSub3(@RequestParam("subjectId") Integer subjectId,@RequestParam("subjectDescription")String subjectDescription){
        Subject sub = new Subject();
        sub.setSubjectId(subjectId);
        sub.setSubjectDescription(subjectDescription);
        try {
            subjectService.update(sub);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:diagnosisUi";
    }
}
